package org.lc.stk.model.question.attrs;

import org.hibernate.annotations.Comment;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

/**
 * 试题状态
 * 定义试题的生命周期状态，包括：已命题、提交待审核、审核中、审核完待入库、已入库、停用。
 * 未入库的试题可以删除，已入库的只能停用。停用状态可以通过再次审核后重新启用。
 */
@Entity
@Table(name = "stk_question_state")
@Comment("试题状态")
public class QuestionState {

    public enum State {
        DRAFTED(1, "已命题", "试题已创建但尚未提交审核", true),
        SUBMITTED(2, "提交待审核", "试题已提交等待审核", true),
        REVIEWING(3, "审核中", "试题正在审核过程中", true),
        REVIEWED(4, "审核完待入库", "试题已审核通过待入库", true),
        ACTIVE(5, "已入库", "试题已入库并可用", false),
        DISABLED(6, "停用", "试题已停用", false);

        private final int value;
        private final String label;
        private final String description;
        private final boolean allowDelete;

        State(int value, String label, String description, boolean allowDelete) {
            this.value = value;
            this.label = label;
            this.description = description;
            this.allowDelete = allowDelete;
        }

        public int getValue() {
            return value;
        }

        public String getLabel() {
            return label;
        }

        public String getDescription() {
            return description;
        }

        public boolean isAllowDelete() {
            return allowDelete;
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 50, nullable = false, unique = true)
    @Comment("状态代码")
    private String code;

    @Column(length = 100, nullable = false)
    @Comment("状态名称")
    private String name;

    @Column(length = 500)
    @Comment("状态描述")
    private String description;

    @Column(nullable = false)
    @Comment("是否允许删除")
    private Boolean allowDelete;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    @Comment("状态枚举值")
    private State state;

    protected QuestionState() {
    }

    public QuestionState(State state) {
        this.state = state;
        this.code = state.name();
        this.name = state.getLabel();
        this.description = state.getDescription();
        this.allowDelete = state.isAllowDelete();
    }

    public Long getId() {
        return id;
    }

    protected void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    protected void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    protected void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getAllowDelete() {
        return allowDelete;
    }

    protected void setAllowDelete(Boolean allowDelete) {
        this.allowDelete = allowDelete;
    }

    public State getState() {
        return state;
    }

    protected void setState(State state) {
        this.state = state;
        this.code = state.name();
        this.name = state.getLabel();
        this.description = state.getDescription();
        this.allowDelete = state.isAllowDelete();
    }

    /**
     * 判断当前状态是否允许删除
     * @return true表示可以删除，false表示不能删除
     */
    public boolean canDelete() {
        return allowDelete;
    }

    /**
     * 判断当前状态是否可以转换到目标状态
     * @param targetState 目标状态
     * @return true表示可以转换，false表示不能转换
     */
    public boolean canTransitionTo(State targetState) {
        if (this.state == targetState) {
            return false;
        }
        
        switch (this.state) {
            case DRAFTED:
                return targetState == State.SUBMITTED;
            case SUBMITTED:
                return targetState == State.REVIEWING || targetState == State.DRAFTED;
            case REVIEWING:
                return targetState == State.REVIEWED || targetState == State.DRAFTED;
            case REVIEWED:
                return targetState == State.ACTIVE || targetState == State.DRAFTED;
            case ACTIVE:
                return targetState == State.DISABLED;
            case DISABLED:
                return targetState == State.REVIEWING;
            default:
                return false;
        }
    }
}
